import rclpy
from rclpy.node import Node
from geometry_msgs.msg import Twist


class Cmd_VelSub(Node):
    def __init__(self):
        super().__init__('Cmd_Vel_Sub')
        #创建订阅者，订阅 /cmd_vel 话题，消息类型为 Twist，回调函数为 self.sub_callback
        self.subscription = self.create_subscription(
            Twist,
            '/cmd_vel',
            self.sub_callback,
            10)

    def sub_callback(self, msg):
        #计算线速度的平方
        linear_speed_squared = msg.linear.x ** 2
        #打印线速度的平方
        self.get_logger().info(f'Linear.x**2 is : {linear_speed_squared}')


def main(args=None):
    rclpy.init(args=args)
    Cmd_Vel_Sub = Cmd_VelSub()
    rclpy.spin(Cmd_Vel_Sub)
    rclpy.shutdown()


if __name__ == '__main__':
    main()
    
